package com.atguigu.dga.governance.assessor.storage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 是否存在相似表
 * 同层次两个表字段重复超过{percent}%，则给0分，其余给10分
 * @Author: lay
 * @Date: 2024/6/28 22:43
 */
@Component("TABLE_SIMILAR")
public class TableSimilarAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        //获取参数
        String paramsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSONObject.parseObject(paramsJson);
        Integer percent = jsonObject.getInteger("percent");
        //表字段
        List<String> currentColNameList = extractTableColName(assessParam.getTableMetaInfo());
        //层级
        String dwLevel = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getDwLevel();

        List<String> similarTableNameList = new ArrayList<>();
        List<TableMetaInfo> tableMetaInfoList = assessParam.getTableMetaInfoList();
        for (TableMetaInfo tableMetaInfo : tableMetaInfoList) {
            //同层级，排除自己
            if (tableMetaInfo.getTableMetaInfoExtra().getDwLevel().equals(dwLevel)
            && !assessParam.getTableMetaInfo().getTableName().equals(tableMetaInfo.getTableName())){
                //其他表字段
                List<String> otherColNameList = extractTableColName(tableMetaInfo);
                // 提取当前表和其他表的重复字段
                Collection duplicationColNameList = CollectionUtils.intersection(currentColNameList, otherColNameList);
                
                if ((duplicationColNameList.size() * 100 / currentColNameList.size()) > percent) {
                    similarTableNameList.add(tableMetaInfo.getTableName());
                }
            }
        }
        
        if (similarTableNameList.size() > 0){
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                governanceAssessDetail.setAssessProblem("存在相似表");
                governanceAssessDetail.setAssessComment("同层次的相似表为: " + similarTableNameList);
        }
    }

    /**
     * 提取表字段
     * @param tableMetaInfo
     * @return
     */
    private List<String> extractTableColName(TableMetaInfo tableMetaInfo) {
        String colNameJson = tableMetaInfo.getColNameJson();
        List<JSONObject> jsonObjectList = JSON.parseArray(colNameJson, JSONObject.class);
        return jsonObjectList.stream().map(jsonObj -> jsonObj.getString("name")).collect(Collectors.toList());
    }
}
